/**
 * \class Coord
 * \brief This is a user interface for space to make the use of spaces simpler.
 * \version 0.3.3
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2008/5/15
 */
// =============================================================
#include "Coord.h"

Coord::Coord()
{
	get_access_point(DEFAULT_CONFIG_FILE);

	wspace = new CWorkSpaces((char *)host.c_str(), port);

	_alive = wspace->alive();
}

Coord::Coord(char *name)
{
	get_access_point(DEFAULT_CONFIG_FILE);

	wspace = new CWorkSpaces((char *)host.c_str(), port, CUBE_SERVICE_SPACE, name);

	_alive = wspace->alive();
}

Coord::Coord(char *name, char *conf)
{
	get_access_point(conf);

	wspace = new CWorkSpaces((char *)host.c_str(), port, CUBE_SERVICE_SPACE, name);

	_alive = wspace->alive();
}

Coord::~Coord()
{
	delete wspace;
}

bool Coord::alive()
{
	return _alive;
}

void Coord::get_access_point(char *conf)
{
        CProperty config(conf);
        pair<string,string> kv;

        kv.first = COORD_CONF_MASTER_IP_KEY;
        host = config.get(kv).second;

        kv.first = COORD_CONF_MASTER_PORT_KEY;
        port = atoi((char *)config.get(kv).second.c_str());
}

void Coord::service(char *s)
{
	wspace->service(s);
}

const char *Coord::service()
{
	return wspace->service();
}

void Coord::instance(char *i)
{
	wspace->instance(i);
}

const char *Coord::instance()
{
	return wspace->instance();
}

CDHT *Coord::topology()
{
	return wspace->topology();
}

int Coord::write(pair<string,string> &kv)
{
	return wspace->write(kv);
}

int Coord::read(pair<string,string> &kv)
{
	return wspace->read(kv);
}

int Coord::readIfExists(pair<string,string> &kv)
{
	return wspace->readIfExists(kv);
}

int Coord::take(pair<string,string> &kv)
{
	return wspace->take(kv);
}

int Coord::takeIfExists(pair<string,string> &kv)
{
	return wspace->takeIfExists(kv);
}

int Coord::bulk(int limit)
{
	return wspace->bulk(limit);
}

int Coord::flush()
{
	return wspace->flush();
}

void Coord::autoclean(bool is_clean)
{
	wspace->autoclean(is_clean);
}

// get size of all the data in local spaces
long Coord::size()
{
	return wspace->size();
}

int Coord::clean(string &key)
{
	wspace->clean(key);
}

int Coord::cleanup()
{
	wspace->cleanup();
}
